home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 7541 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.7 KB

  1. Path: sargas.omicron.se!usenet
  2. From: elias@cepheus.omicron.se (Elias Martenson)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: returning ptr to struct with ptrs in it?
  5. Date: 27 Feb 1996 10:31:51 GMT
  6. Organization: Omicron
  7. Message-ID: <ELIAS.96Feb27113151@cepheus.omicron.se>
  8. References: <4gk852INNbp4@faatcrl.faa.gov>
  9. NNTP-Posting-Host: graffias.omicron.se
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=US-ASCII
  12. Content-Transfer-Encoding: 7bit
  13. In-reply-to: lbona@saratoga's message of 23 Feb 1996 11:24:18 GMT
  14.  
  15. In article <4gk852INNbp4@faatcrl.faa.gov> lbona@saratoga (lbona) writes:
  16.  
  17.  
  18. > When I declare a function that returns a pointer to a struct that contains 
  19. > pointers, the pointer(s) at the end of the struct get set to garbage after 
  20. > being returned. Pointers not at the end are not affected.
  21. >     /* toke is an ascii string read in from a file */
  22. >   BB = *parse_bar(toke);
  23. >   /* at this point BB.g and BB.h are garbage, but all the other fields */
  24. >   /* in BB are correct including BB.d*/
  25. > }
  26. > BAR *parse_bar(char toke[]);
  27. > {
  28. > BAR bb;
  29. >   memset(&bb,0,sizeof(BAR));
  30. >    /* at this point BB.d == BB.g == BB.h == NULL */
  31. >   /* read data from file and parse - never touch bb.d, bb.g, or bb.h */
  32. >    /* at this point BB.d == BB.g == BB.h == NULL */
  33. >   return(&bb);
  34. > }
  35.  
  36. Woow, haven't I seen this one a lot of times...
  37.  
  38. You are declaring bb in parse_par() as being an auto. That means that it's
  39. contents are no longer valid when parse_bar() exists. Change it's definition
  40. from "BAR bb;" to "static BAR bb;" and it'll work.
  41.  
  42. Regards /
  43.         Elias
  44.  
  45. -- 
  46. Elias Martenson                            ! When I come up with a good joke,
  47. elias@omicron.se                           ! it will be here.
  48.